//
//  NewDocumentController.m
//  Mr.Java
//
//  Created by Justin Brower on 7/16/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "NewDocumentController.h"

@interface NewDocumentController ()

@end

@implementation NewDocumentController
@synthesize ref;
- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}
- (id)init
{
    self = [super initWithWindowNibName:@"NewDocument"];
    
    return self;
    
}

- (IBAction)searchDir:(id)sender
{
    
    NSOpenPanel *panel = [[NSOpenPanel alloc] init];
    [panel setCanChooseFiles:NO];
    [panel setCanChooseDirectories:YES];
    [panel setCanCreateDirectories:YES];
    
    [panel setPrompt:@"Choose a Directory"];
    
   int i = [panel runModal];
    if ( i == NSOKButton )
    {
        NSURL *m = [[panel URLs] objectAtIndex:0];
        
        NSString *dir = [m path];
        [mainDir setStringValue:dir];
    }
    
    
}

- (IBAction)create:(id)sender
{
    if ( [mainDir stringValue].length == 0 )
    {
        NSAlert *alert = [NSAlert alertWithMessageText:@"Error!" defaultButton:@"Okay" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"Please select a path for your application!"];
        [alert runModal];
        return;
        
    }
    
    if ( [projectName stringValue].length == 0 )
    {
        NSAlert *alert = [NSAlert alertWithMessageText:@"Error!" defaultButton:@"Okay" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"Please select a name for your application -- you can always change this later!"];
        [alert runModal];
        return;
        
    }
    NSSavePanel *savePanel = [NSSavePanel savePanel];
    [savePanel setCanCreateDirectories:YES];
  
    NSString *filename; NSURL *dump;
    if ( [savePanel runModal] == NSOKButton )
    {
        filename = [[savePanel URL] path];
        dump = [savePanel URL];
        [dump retain];
        if ( ![filename hasSuffix:@".mrj"] )
        {
            filename = [[filename stringByAppendingString:@".mrj"] retain];
        }
        
        
        
    }
    
    
    
    
    NSString *mainC = ([mainClass stringValue].length == 0) ? @"^&__NONE__&^" : [mainClass stringValue];
    
    NSMutableString *fileContents = [[NSMutableString alloc] init];
    
    [fileContents appendString:@"# Generated by Mr.Java 1.0\n"];
    [fileContents appendString:@"mrj100\n"];
    [fileContents appendFormat:@"%@\n",[projectName stringValue]];
    [fileContents appendFormat:@"%@\n",mainC];
    [fileContents appendFormat:@"%@\n",[mainDir stringValue]];
    [fileContents appendFormat:@"mrj100"];

    if ( ![[NSFileManager defaultManager] fileExistsAtPath:filename] )
    {
    
    [[NSFileManager defaultManager] createFileAtPath:filename contents:[fileContents dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    }
    else {
        
        [[NSFileManager defaultManager] removeItemAtURL:[NSURL URLWithString:filename] error:nil];
        [[NSFileManager defaultManager] createFileAtPath:filename contents:[fileContents dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
        
        
    }
    
    
    [ref loadDocumentWithFilename:dump];
    
    [self close];
    
    
}

- (void)windowDidLoad
{
    [super windowDidLoad];
    
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

@end
